<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of message_m
 *
 * @author Gidaff_01
 */
class Message_m extends Model {

    //put your code here
    function Message_m() {
        parent::Model();
        $this->load->database();
        $this->load->library('session');
    }

    function send_message($data) {
        $query = $this->db->insert('doggy_messages', $data);
        return $query;
    }
    function get_message_details($message_id){
        $this->db->select('doggy_messages.id,doggy_messages.title,doggy_messages.content,doggy_messages.posted_by,doggy_profile.display_name,doggy_profile.avatar,doggy_messages.posted_date');
        $this->db->from('doggy_messages,doggy_profile');
        $this->db->where('doggy_messages.posted_by = doggy_profile.account_id');
        $this->db->where(array('doggy_messages.id'=>$message_id));
        return $this->db->get();    
    }
    function list_all_messages($to_friends, $per_page, $off_set) {
        $this->db->select('doggy_messages.posted_date,doggy_profile.display_name,doggy_profile.avatar,doggy_messages.id,doggy_messages.title,doggy_messages.content,doggy_messages.posted_by,doggy_accounts.account_name,doggy_messages.is_read');
        $this->db->from('doggy_accounts ,doggy_messages,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id = doggy_messages.posted_by');
        $this->db->where('doggy_messages.id NOT IN (Select doggy_messages_delete.message_id from doggy_messages_delete Where doggy_messages_delete.hidden_to='.$to_friends.')');
        $this->db->where('(doggy_messages.to_friends = '.$to_friends.' or doggy_messages.posted_by='.$to_friends.')');
        $this->db->order_by("doggy_messages.id", "desc");
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_all_messages($to_friends) {
        $this->db->select('doggy_messages.posted_date,doggy_profile.display_name,doggy_profile.avatar,doggy_messages.id,doggy_messages.title,doggy_messages.content,doggy_messages.posted_by,doggy_accounts.account_name,doggy_messages.is_read');
        $this->db->from('doggy_accounts ,doggy_messages,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id = doggy_messages.posted_by');
        $this->db->where('doggy_messages.id NOT IN (Select doggy_messages_delete.message_id from doggy_messages_delete Where doggy_messages_delete.hidden_to='.$to_friends.')');
        $this->db->where('doggy_messages.to_friends = ', $to_friends);
        $this->db->order_by("doggy_messages.id", "desc");
        $query = $this->db->get();
        return count($query->result_array());
    }

    function count_unread_messages($to_friends) {
        $this->db->select();
        $this->db->from('doggy_accounts ,doggy_messages');
        $this->db->where('doggy_accounts.id = doggy_messages.posted_by');
        $this->db->where('doggy_messages.to_friends = ', $to_friends);
        $this->db->where('doggy_messages.id NOT IN (Select doggy_messages_delete.message_id from doggy_messages_delete Where doggy_messages_delete.hidden_to='.$to_friends.')');
        $this->db->where('doggy_messages.is_read = 0');
        $query = $this->db->get();
        return count($query->result_array());
    }
    function count_unread_reply($sender_id){
        $count_unread = $this->db->select('doggy_message_comments.id')
                        ->from('doggy_message_comments')
                        ->where(array('is_read'=>0,'account_id <>'=>$sender_id))
                        ->where('doggy_message_comments.message_id IN (Select doggy_messages.id FROM doggy_messages WHERE doggy_messages.posted_by = '.$sender_id.' OR doggy_messages.to_friends = '.$sender_id.')')
                        ->get()
                        ->result_array();
        //echo count($count_unread);die;
        return count($count_unread);
        
    }
    function set_read_reply($messaage_id){
        $self = $this->session->userdata('doogy.id');
        return $this->db->update('doggy_message_comments',array('is_read'=>1),array('message_id'=>$messaage_id,'account_id <>'=>$self));
    }
    function read_message($id) {
        $this->db->select('doggy_messages.posted_date,doggy_profile.display_name,doggy_profile.avatar,doggy_messages.id,doggy_messages.title,doggy_messages.content,doggy_messages.posted_by,doggy_accounts.account_name,doggy_messages.is_read');
        $this->db->from('doggy_accounts ,doggy_messages,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id = doggy_messages.posted_by');
        $this->db->where('doggy_messages.id = ', $id);
        $query = $this->db->get();
        return $query;
    }

    function is_read($data, $id) {
        return $this->db->update('doggy_messages', $data, array('id' => $id));
    }

    function delete_message($id,$account_id=0) {
        $this->db->insert('doggy_messages_delete',array('message_id'=>$id,'hidden_to'=>$account_id),array('id' => $id));
        $comments = $this->db->get_where('doggy_message_comments',array('message_id'=>$id))->result_array();
        foreach($comments as $comment){
            $this->set_hidden_message($comment['id'],$account_id);
            /*select message_comment_id if = 2 then delete from doggy_messages_comment_delete*/    
        }
        /*select message_id if = 2 then delete from doggy_messages_delete*/
        //$this->db->count_all_results();
        $message_id_hidden_counnt = count($this->db->get_where('doggy_messages_delete',array('message_id'=>$id))->result_array());
        if($message_id_hidden_counnt>1){
            if($this->db->delete('doggy_messages_delete',array('message_id'=>$id))>0){
                $this->db->delete('doggy_messages',array('id'=>$id));
            }    
        }
    }
    function set_hidden_message($message_id,$hidden_to){
        $this->db->insert('doggy_message_comments_delete',array('reply_id'=>$message_id,'hidden_to'=>$hidden_to));
        $comment_id_hidden_counnt = count($this->db->get_where('doggy_message_comments_delete',array('reply_id'=>$message_id))->result_array());
        if($comment_id_hidden_counnt>1){
            $this->db->delete('doggy_message_comments_delete',array('reply_id'=>$message_id));
            if($this->db->delete('doggy_message_comments_delete',array('reply_id'=>$message_id))>0){
                $this->db->delete('doggy_message_comments',array('id'=>$message_id));
            }   
        }    
    }

    function search_friend($key_word, $account_id, $per_page, $off_set) {
        $key_word = mysql_escape_string($key_word);
        $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.avatar');
        $this->db->from('doggy_accounts ,doggy_profile ');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id <> ', $account_id);
        $this->db->where('doggy_accounts.id NOT IN (Select doggy_friend_requests.to_account from doggy_friend_requests WHERE doggy_friend_requests.from_account=' . $account_id . ')');
        $this->db->where('doggy_accounts.id NOT IN (Select doggy_friends.friend_id from doggy_friends WHERE doggy_friends.account_id=' . $account_id . ')');
        //$this->db->like('doggy_profile.fullname', $key_word);
        $this->db->where("(doggy_profile.fullname LIKE '%".$key_word."%' OR doggy_profile.display_name LIKE '%".$key_word."%' OR doggy_accounts.account_name LIKE '%".$key_word."%') ");
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_search_friend($key_word, $account_id) {
        $key_word = mysql_escape_string($key_word);
        $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.avatar');
        $this->db->from('doggy_accounts ,doggy_profile ');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id <> ', $account_id);
        $this->db->where('doggy_accounts.id NOT IN (Select doggy_friend_requests.to_account from doggy_friend_requests WHERE doggy_friend_requests.from_account=' . $account_id . ')');
        $this->db->where('doggy_accounts.id NOT IN (Select doggy_friends.friend_id from doggy_friends WHERE doggy_friends.account_id=' . $account_id . ')');
        $this->db->where("(doggy_profile.fullname LIKE '%".$key_word."%' OR doggy_profile.display_name LIKE '%".$key_word."%' OR doggy_accounts.account_name LIKE '%".$key_word."%') ");
        //$this->db->like('doggy_profile.fullname', $key_word);
        $query = $this->db->get();
        return count($query->result_array());
    }

    function send_wall_message($data) {
        //print_r($data);die;
        $query = $this->db->insert('doggy_wall_messages', $data);
        return $query;
    }

    function reply_message($data){
        $query = $this->db->insert('doggy_message_comments', $data);
        return $query;
    }

    function all_reply($message_id,$per_page, $off_set){
        //$self = $this->session->userdata('doogy.id');
        $this->db->select('
            doggy_message_comments.id,
            doggy_profile.display_name,
            doggy_profile.avatar,
            doggy_messages.title,
            doggy_messages.posted_date,
            doggy_messages.is_read,
            doggy_messages.posted_by,
            doggy_message_comments.content,
            doggy_message_comments.message_id,
            doggy_message_comments.account_id,
            doggy_message_comments.commented_time
        ');
        $this->db->from('doggy_accounts ,doggy_messages,doggy_profile,doggy_message_comments');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id = doggy_message_comments.account_id');
        $this->db->where('doggy_message_comments.message_id = doggy_messages.id');
        $this->db->where('doggy_message_comments.message_id = ', $message_id);
        $this->db->order_by('doggy_message_comments.commented_time','DESC');
        //$this->db->where('doggy_message_comments.account_id NOT IN (SELECT doggy_message_comments_delete.hidden_to FROM doggy_message_comments_delete WHERE doggy_message_comments_delete.reply_id = doggy_message_comments.id)');
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_all_reply($message_id){
        $this->db->select('
            doggy_message_comments.id,
            doggy_profile.display_name,
            doggy_profile.avatar,
            doggy_messages.title,
            doggy_messages.posted_date,
            doggy_messages.is_read,
            doggy_messages.posted_by,
            doggy_message_comments.content,
            doggy_message_comments.message_id,
            doggy_message_comments.account_id,
            doggy_message_comments.commented_time
        ');
        $this->db->from('doggy_accounts ,doggy_messages,doggy_profile,doggy_message_comments');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
       $this->db->where('doggy_accounts.id = doggy_message_comments.account_id');
        $this->db->where('doggy_message_comments.message_id = doggy_messages.id');
        $this->db->where('doggy_message_comments.message_id = ', $message_id);
        $query = $this->db->get();
        return count($query->result_array());
    }

    function get_reply_id_max() {
            $this->db->select('MAX(id) as new_id');
            $query = $this->db->get('doggy_messages');
            return $query;
        }

}

?>
